package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.MapBottomTopicAdapter;
import com.srwl.mytx.domain.Topic;

import java.util.List;

public class BottomTopicList extends LinearLayout {
    private static final String TAG = "BottomTopicList";
    private InteractionListener listener;
    private List<Topic> topicList;
    RecyclerView rv_dialogItems;
    public MapBottomTopicAdapter topicAdapter;
    private Context context;
    private BottomSheetBehavior<View> bottomSheetBehavior;

    public BottomTopicList(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }


    public BottomTopicList(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public BottomTopicList(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public BottomTopicList(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
    }

    private void init() {
        inflate(context, R.layout.bottom_dialog, this);
        rv_dialogItems = findViewById(R.id.rv_dialog);

        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper() {
            @Override
            public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
                int targetSnapPosition = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
                if (listener != null) {
                    listener.onPageChange(targetSnapPosition);
                }
                return targetSnapPosition;
            }
        };
        pagerSnapHelper.attachToRecyclerView(rv_dialogItems);
    }

    public void init(List<Topic> topicList) {
        this.topicList = topicList;
        topicAdapter = new MapBottomTopicAdapter(context, topicList);
        rv_dialogItems.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
        rv_dialogItems.setAdapter(topicAdapter);
    }

    public void setBottomSheetBehavior(BottomSheetBehavior<View> bottomSheetBehavior) {
        this.bottomSheetBehavior = bottomSheetBehavior;
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int newState) {
                Log.d(TAG, "onStateChanged: " + newState);
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    setVisibility(GONE);
                    if (listener != null) {
                        listener.onHide();
                    }
                }
            }

            @Override
            public void onSlide(@NonNull View view, float slideOffset) {
                Log.d(TAG, "onSlide: " + slideOffset + "view.getTop" + view.getTop());

                float scrollY = ((1 + slideOffset) * view.getHeight());
                if (listener != null && !Float.isNaN(scrollY)) {
                    listener.onScrolled(scrollY);
                }
            }
        });
    }

    public void init(int position) {
        rv_dialogItems.scrollToPosition(position);
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public void show(int position) {
        setVisibility(VISIBLE);
        topicAdapter.notifyDataSetChanged();
        rv_dialogItems.scrollToPosition(position);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        bottomSheetBehavior.setSkipCollapsed(false);
    }

    public boolean hide() {
        if (bottomSheetBehavior == null) {
            return false;
        }
        if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED || bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            setVisibility(GONE);
            return true;
        }
        return false;
    }

    public void notifyDataSetChange() {
        topicAdapter.notifyDataSetChanged();
    }

    public void notifyItemChange(int position) {
        topicAdapter.notifyItemChanged(position);
    }

    public void notifyItemRemove(int position) {
        topicAdapter.notifyItemRemoved(position);

    }

    public interface InteractionListener {
        void onPageChange(int position);

        /**
         * @param dy Y方向滑动的距离
         */
        void onScrolled(float dy);

        void onHide();
    }
}
